<template>
  <el-dialog
    :visible.sync="visible"
    width="50%"
    :title="formData.admissionId ? '编辑专业' : '新增专业'"
    top="20px"
    :close-on-click-modal="false"
  >
    <el-form
      ref="ruleForm"
      :model="formData"
      :inline="false"
      :rules="rules"
      label-width="0"
      class="demo-ruleForm"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <p class="form_required">大学</p>
          <el-form-item prop="uniName">
            <el-input
              v-model="formData.uniName"
              readonly
              placeholder="放大镜选择大学"
              size="medium"
              clearable
            >
              <el-button
                slot="append"
                type="primary"
                icon="el-icon-search"
                @click="search"
              ></el-button>
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <p class="form_required">专业类别</p>
          <el-form-item prop="majorName">
            <el-input
              v-model="formData.majorName"
              readonly
              placeholder="放大镜选择专业"
              size="medium"
              clearable
            >
              <el-button
                slot="append"
                type="primary"
                icon="el-icon-search"
                @click="searchMajor"
              ></el-button>
            </el-input>
          </el-form-item>
        </el-col>

        <!-- <el-col :span="12">
          <p class="form_required">实力层次</p>
          <el-form-item prop="subjectLevelList">
            <el-select
              type="tags"
              multiple
              clearable
              class="filter-item"
              :remote="true"
              v-model="formData.subjectLevelList"
              filterable
              placeholder="请选择实力层次"
            >
              <el-option
                v-for="(item, index) in subjectLevelList"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <p class="form_required">年份</p>
          <el-form-item prop="subjectLevelList">
            <el-select
              clearable
              class="filter-item"
              v-model="formData.year"
              placeholder="请选择年份"
            >
              <el-option
                v-for="(item, index) in years"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <selectSchoolDialog
        ref="selectSchoolDialog"
        @ok="selectOK"
        :appendToBody="true"
      ></selectSchoolDialog>
      <selectMajorlDialog
        ref="selectMajorlDialog"
        @ok="majorOK"
        :appendToBody="true"
      ></selectMajorlDialog>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="confirm(true)" :loading="submitLoading"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { uploadFile } from "@/api/admin.js";
import { getInfoById, add } from "@/api/web/major-school";
import selectSchoolDialog from "@/views/pages/school/modual/selectSchoolDialog";
import selectMajorlDialog from "./selectMajorlDialog";
import { getYears } from "@/utils/gaobao";
export default {
  components: { selectSchoolDialog, selectMajorlDialog },
  data() {
    const checkList = (rule, value, callback) => {
      console.log('value :>> ', value);
        if (!value.length) {
          return callback(new Error('请选择实例标签'));
        }
        callback()
    }
    return {
      submitLoading: false,
      loading: false,
      formData: {},
      bgImgUrl_loading: false,
      emblem_loading: false,
      teacherPhotos: [],
      enrollmentTypes: [
        { label: "停招院校", value: 1 },
        { label: "新增院校", value: 2 },
        { label: "特殊院校", value: 3 },
        { label: "校考院校", value: 4 },
      ],
      subjectLevelList: [
        { label: "国家重点学科", value: 1 },
        { label: "双一流学科", value: 2 },
        { label: "特色专业", value: 3 },
      ],
      years: [],
      uniMajorList: [],
      visible: false,

      rules: {
        majorName: [
          {
            required: true,
            message: "请选择专业类型",
          },
        ],
        uniName: [
          {
            required: true,
            message: "请选择大学",
          },
        ],
        subjectLevelList:[
            { validator: checkList, trigger: 'blur' }
        ]
      },
    };
  },
  mounted() {
    if (this.$route.query.id) {
      this.getInfoById();
    }
  },
  methods: {
    add() {
      this.edit({});
    },
    edit(record) {
      this.visible = true;
      this.formData = record;
      this.$set(this.formData, "uniName", record.uniName);
      this.$set(this.formData, "uniId", record.uniId);
      this.years = getYears();
      if (this.formData.uniMajorId) {
        this.getInfoById()
      }
    },
    search() {
      this.$refs.selectSchoolDialog.add();
    },
    searchMajor() {
      this.$refs.selectMajorlDialog.add();
    },
    selectOK(uniObj) {
      this.uniObj = uniObj;
      this.$set(this.formData, "uniName", uniObj.uniName);
      this.$set(this.formData, "uniId", uniObj.uniId);
    },
    majorOK(majorObj) {
      this.majorObj = majorObj;
      this.$set(this.formData, "majorName", majorObj.majorName);
      this.$set(this.formData, "majorId", majorObj.majorId);
    },
    
    getInfoById() {
      this.loading = true
      getInfoById({ uniMajorId: this.formData.uniMajorId }).then(res => {
        this.loading = false
        if (res.code == 200) {
          this.formData = Object.assign(this.formData,res.data)
          console.log('this.formData :>> ', this.formData);
          this.$set(this.formData,'year',+this.formData.year)
          this.$forceUpdate()
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
      })
    },
    uploadRequest(params, key) {
      this[`${key}_loading`] = true;
      uploadFile(params).then((res) => {
        this[`${key}_loading`] = false;
        if (res && res.data && res.data.length) {
          if (key === "teacher") {
            this.teacherPhotos.push(res.data[0]);
          } else {
            this.$set(this.formData, key, res.data[0]);
          }
        }
      });
    },
    /**
     * 保存
     */
    confirm(closeDialog) {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          add(this.formData).then((res) => {
            this.submitLoading = false;
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "提交成功",
              });
              this.$emit("ok", this.formData);
              this.visible = false;
            } else {
              this.$message({
                type: "error",
                message: res.message,
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
  
<style scoped>
.avatar-uploader,
.el-upload {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px !important;
  height: 178px;
}

.avatar-uploader:hover,
.el-upload:hover {
  border-color: #409eff;
}

.upload-text {
  font-size: 14px;
  color: #8c939d;
  line-height: 178px;
  text-align: center;
  margin: 0 auto;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.container {
  padding-bottom: 30px;
}

.t_img {
  display: inline-block;
  width: 178px;
  height: 178px;
  margin-right: 12px;
  margin-top: 12px;
}

.form_required::before {
  content: "*";
  color: red;
}
</style>
<style>
.demo-ruleForm .el-input,
.demo-ruleForm .el-select {
  width: 300px;
}

.el-dialog__header {
  border-bottom: 1px solid #e8e8e8;
}

.el-dialog__footer {
  padding: 20px !important;
  border-top: 1px solid #e8e8e8;
}
</style>
  